@echo off

::python Python_CopyFile.py %*

Set configPath={0}
Set sourceDir={1}
Set targetDir={2}
Set pythonPath="{3}"
Set isComment=False
Set isAddToday=True
Set isBKWording="{4}"
Set isDeployPackage="{6}"
Set curDate={7}
Set packageEnv="{8}"
Set version=V{9}
Set sEnv=Production
If %packageEnv% == "preview" (
    Set sEnv=Preview
)
：：Set rar="C:\Program Files\7-Zip\7z"
Set rar="C:\Program Files\WinRAR\WinRAR"
Set wordingFilePath="D:\\Tools\\JobsDBTool\\Resource\\InputData.csv"

Set LogFile={5}.log
Set TempFile={5}.tmp
Set "RedirectLogCommand=>%TempFile% & type %TempFile% & type %TempFile% > %LogFile%"

echo. %RedirectLogCommand%
echo Copy JobsDB BK to BKFolder... %RedirectLogCommand%

If %isDeployPackage% == "True" (
  echo "delete last folder"
  rd /s/q  "%targetDir%\_%version%"
  rd /s/q  "%targetDir%\Local Deploy Package\Deploy_%curDate%_HK"
  rd /s/q  "%targetDir%\Local Deploy Package\Deploy_%curDate%_TH"
  rd /s/q  "%targetDir%\Local Deploy Package\Deploy_%curDate%_ID"
  Set isAddToday=False
)
if ERRORLEVEL 1 goto error

If %isBKWording% == "True" (
    python %pythonPath% %configPath% %sourceDir% %targetDir% %isComment% %isAddToday% %RedirectLogCommand%
) else (
    python %pythonPath% %configPath% %sourceDir% %targetDir% %isComment% %isAddToday% %isBKWording% %wordingFilePath% %RedirectLogCommand%
)
if ERRORLEVEL 1 goto error

If %isDeployPackage% == "True" (
  echo "processing config file"
  If exist "%targetDir%\_%version%\JobsDB.Web\config\FileSystemProperty.config" (
    echo "backup config folder"
    echo d|xcopy "%targetDir%\_%version%\JobsDB.Web\Config" "%targetDir%\_%version%\Config"
    rd /s/q "%targetDir%\_%version%\JobsDB.Web\Config"
    If %packageEnv% == "prod" (
        echo f|xcopy "%targetDir%\_%version%\Config\FileSystemProperty.Prod.config" "%targetDir%\_%version%\JobsDB.Web\Config\FileSystemProperty.config"
    ) else (
        echo f|xcopy "%targetDir%\_%version%\Config\FileSystemProperty.Preview.config" "%targetDir%\_%version%\JobsDB.Web\Config\FileSystemProperty.config"
    )
  )
  if ERRORLEVEL 1 goto error
  
  echo "compression to rar file"
  cd /d %targetDir%\_%version%\
  echo %curDate%_%sEnv%>webDeployment.log
  %rar% a -r %targetDir%\_%version%\CFS_Web_%sEnv%.rar JobsDB.Web webDeployment.log
  
  echo "copy rar to each countries"
  echo f|xcopy "%targetDir%\_%version%\CFS_Web_%sEnv%.rar" "%targetDir%\Local Deploy Package\Deploy_%curDate%_HK\CFS_Web_%sEnv%_HK.rar"
  echo f|xcopy "%targetDir%\_%version%\CFS_Web_%sEnv%.rar" "%targetDir%\Local Deploy Package\Deploy_%curDate%_TH\CFS_Web_%sEnv%_TH.rar"
  echo f|xcopy "%targetDir%\_%version%\CFS_Web_%sEnv%.rar" "%targetDir%\Local Deploy Package\Deploy_%curDate%_ID\CFS_Web_%sEnv%_ID.rar"

  echo "copy packages to 50"
  echo "copy packages to 50"
  net use /delete \\10.1.9.50\d$ /Y
  net use \\10.1.9.50\d$ Aa123456 /user:.\developer
  xcopy  /s /e /c /y /i "%targetDir%\Local Deploy Package\Deploy_%curDate%_HK\CFS_Web_%sEnv%_HK.rar" "\\10.1.9.50\D$\Upload AutoDeploy Zip\Deploy_%curDate%_HK\"
  xcopy  /s /e /c /y /i "%targetDir%\Local Deploy Package\Deploy_%curDate%_TH\CFS_Web_%sEnv%_TH.rar" "\\10.1.9.50\D$\Upload AutoDeploy Zip\Deploy_%curDate%_TH\"
  xcopy  /s /e /c /y /i "%targetDir%\Local Deploy Package\Deploy_%curDate%_ID\CFS_Web_%sEnv%_ID.rar" "\\10.1.9.50\D$\Upload AutoDeploy Zip\Deploy_%curDate%_ID\"
  
  echo "delete temp rar"
  del /s/q  "%targetDir%\_%version%\CFS_Web_%sEnv%.rar"
  echo "rollback config"
  If exist "%targetDir%\_%version%\JobsDB.Web\config\FileSystemProperty.config" (
    echo "backup config folder"
    echo a|xcopy "%targetDir%\_%version%\Config" "%targetDir%\_%version%\JobsDB.Web\Config"
    rd /s/q "%targetDir%\_%version%\Config"
  )
)
if ERRORLEVEL 1 goto error

goto eof

:error
ECHO.
ECHO. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ECHO. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ECHO. !! Hit an error.  Pls check.     !!
ECHO. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ECHO. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ECHO.
goto eof

:eof
if Exist %TempFile% Del %TempFile%
pause & exit
